"""author: wenyao
   data: 2021/4/23
   project: devopscmdb
"""
#  高阶函数
# • 把函数作为参数传入，这样的函数称为高阶函数
#
# def func01(f,name):
#     f(name)
#
# def func02(name):
#     print(f"my name is {name}")
#
# func01(func02,"wen")

############常用的高阶函数
#map 映射
# def func(item):
#     return item*item
#
# a = [1,2,3,4,5,6]
# result = map(func, a)
# print(list(result))
# result2 = map(lambda item:item*item, a)
# print(list(result2))

# def func(a,b):
#     return a+b
#
# lst1 = [1,2,3,4,5,6]
# lst2 = [6,7,8,9,10]
# result = map(func,lst1,lst2)
# print(list(result))


# 有列表[1,2,3,4,5]，将所有元素转换成str:['1','2','3','4','5']
# lst = [1,2,3,4,5]
# print(list(map(str,lst)))
#  有列表字符串'span'，将各字符转换成对应的ascii码的列表[115, 112, 97, 109]
# s = "span"
# print(list(map(ord,s)))
#  有列表[-1,-2,0,1,2]，将各元素转换成绝对值[1, 2, 0, 1, 2]
# lst2 = [-1,-2,0,1,2]
# print(list(map(abs,lst2)))
#
# lst1 = [0.234573124,0.58331,0.964234]
#转化为百分比，保留两位小数
#23.46%
#print(list(map(lambda x:"%.2f%%"%(x*100),lst1)))
#print(list(map(lambda x:round(x*100,2),lst1)))

#####filter
#过滤的
#把可迭代对象里的每个数交给指定函数去处理
#如果函数返回值为真就保留这个元素，如果函数返回值为假，就剔除这个元素
# a = [1,2,3,4,5,6,7]
# def func1(x):
#     return x%2 == 1
# result = filter(func1,a)
# print(list(result))

# 把一个序列中的空白字符串还有为假的元素删除，
# lst =  ['A', '', 'B', None, 'C', '     ', 'a', 1, 0]
# # • ['A', 'B', 'C', 'a', 1]
# print(list(filter(lambda x:x and str(x).strip(),lst)))
#
# #过滤出100以内开平方是整数的数
# import math
# print(list(filter(lambda x:math.sqrt(x) in range(1,11),range(101))))
#
# #过滤出100-999以内的水仙花数
# #153 = 1**3 + 5**3 + 3**3
# def func(x):
#     num = str(x)
#     return int(num[0])**3 +int(num[1])**3 +int(num[2])**3 == x
# print(list(filter(func,range(100,1000))))

##########reduce
# from functools import reduce
# a = [1,2,3,4]  #-->1234
# def func1(x,y):
#     return x*10 +y
# print(reduce(func1,a))
#
# print(reduce(lambda x,y: str(x) + str(y),a))

##自己实现int
#"2"  --> 2
#dict1 = {"1":1,"2":2}

## 将字符串的数字转换为整型的数字
# num = input("请输入一个数字：")
# dic = {"1":1,"2":2,"3":3,"4":4,"5":5}
# def func(x,y):
#     return dic.get(x,x)*10 + dic.get(y,y)
# if len(num) ==1:
#     print(dic.get(num,num),type(dic.get(num,num)))
# else:
#     print(reduce(func,num),type(reduce(func,num)))

##########sorted
#排序
#
# a = [13,2,5,3,6,9]
# # a.sort()
# # print(a)
# print(sorted(a))
#
# a = [-5,6,-2,9,-18]
# print(sorted(a,key=abs,reverse=True))
#
# a= ["abc","1","3435","234","aa"]
# print(sorted(a,key=len))
#
# a = ['bob', 'about', 'Zoo', 'Credit']
# print(sorted(a,key=str.upper))

#字典排序
# a={"s":1,"b":0,"c":2,"x":-1}
#
# # for i,j in a.items():
# #("s",1) ("b",0)
# print(dict(sorted(a.items(), key=lambda x:x[1])))

#布尔类型排序
# print(True+True)

# a b c d 四名犯罪嫌疑人， 其中有一个是小偷。
# 受到审讯时：
#  a说 我不是小偷
#  b说 c是小偷
#  c说 小偷是d
#  d说 c胡说
#其中三个说实话，一个人说假话，求问犯人是谁。
# for x in ["a","b","c","d"]:
#     if ((x !="a") + (x=="c") + (x=="d") + (x !="d")) ==3:
#         print(x)

# a = [0,True,1,False]
# print(sorted(a))

#列表中元组的排序，先按照第一个元素进行排序，然后按照第二个元素进行排序
# a = [(1,2),(2,2),(1,0),(2,4),(True,False)]
# print(sorted(a))

# lst = ["123","wy","1234wy"]


